﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace B2B.Controllers
{
    public class ProductController : AlibabaApplicationController
    {
        //
        // GET: /Product/

        public ActionResult ProductList(int id)
        {
            ViewBag.DanhMuc = id;
            int tongPage = 0;
            Models.SP_DanhSachSanPham sp_DanhSachSanPham = new Models.SP_DanhSachSanPham();
            ArrayList lis_SP = sp_DanhSachSanPham.DanhSachSanPhamTheoDanhMuc(id, 0, out tongPage);
            ViewData["listSP_TheoDM"] = lis_SP;
            int tongSP = sp_DanhSachSanPham.TongSoSanPham(id);
            ViewData["tongSP"] = tongPage;
            return View();
        }

        public JsonResult ProductListPhanTrang(int maDM, int page)
        {
            int tongPage = 0;
            Models.SP_DanhSachSanPham sp_DanhSachSanPham = new Models.SP_DanhSachSanPham();
            ArrayList lis_SP = sp_DanhSachSanPham.DanhSachSanPhamTheoDanhMuc(maDM, page, out tongPage);

            return Json(lis_SP, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ProductDetail(int id)
        {
            Models.SP_ChiTietSanPham chitietSP = new Models.SP_ChiTietSanPham();
            ViewData["chitietSP"] = chitietSP.chiTietSanPham(id);
            return View();
        }
        [ValidateInput(false)]
 
        public ActionResult ProductPostBuyRequest(Models.PostBuyRequest id, HttpPostedFileBase fileUpload)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
            int kq = 0;
            if (fileUpload != null)
            {
                var fileName = Path.GetFileName(fileUpload.FileName);
                var path = Path.Combine(Server.MapPath("~/Images/Product/"), fileName);
                fileUpload.SaveAs(path);
                kq = id.ThemSanPhamMua(id, fileName);
            }

            return View();
        }


  /*      public ArrayList LaySanPhamMuaTheoMa(int id)
        {
            Models.PostBuyRequest pbr = new Models.PostBuyRequest();
            ArrayList list = pbr.LaySanPhamMuaTheoMa(id);
            return list;
        }
        */
       

        public ActionResult ProductSend(int id, string tenSP, string email, string hoTen, string hinhAnh)
     //   public ActionResult ProductWatchPostBuyRequest(int id, int page, int XoaMaSanPhamMua)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");
            ViewData["maSP"] = id.ToString();
            ViewData["tenSP"] = tenSP;
            ViewData["email"] = email;
            ViewData["hoTen"] = hoTen;
            ViewData["hinhAnh"] = hinhAnh;

            return View();
        }

     
        
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult SendEmailContact(string id, string maSP, Models.SP_ThemSendContact sp_SendContact)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");

            string link = "/Product/ProductDetail/" + maSP;
            sp_SendContact.ThemSendEmailContact(sp_SendContact, id, link);
            return View("ProductPostBuyRequest");
        }

        public ActionResult ProductWatchPostBuyRequest(int id)
        {
            //Kiem tra session
            if (Session["email"] == null)
                return RedirectToAction("NotLogin", "Home");

            Models.PostBuyRequest pbr = new Models.PostBuyRequest();
          /*  if (XoaMaSanPhamMua != 0)
            {
                pbr.XoaSanPhamMua(XoaMaSanPhamMua);
            }*/
//ViewData["page"] = page;
        //    page--;
       //     int SoLuongDong = 5;
       //     int DongBatDau = page * SoLuongDong;
            ArrayList list = pbr.DanhSachSanPhamMua(id);
            ViewData["ds_SanPhamMua"] = list;
        //    ViewData["tongDong"] = pbr.TongSanPhamTheoMa(id);
            ViewData["tongDong"] = list.Count;
            return View();
            //return Json(list, JsonRequestBehavior.AllowGet);
        }

        

      /*  public ActionResult ProductUpdatePostBuyRequest(int  id, string TenHinh, int MaSanPhamMua)
        {
            if (id.HinhAnh != null) 
            {
                string path = ControllerContext.HttpContext.Server.MapPath(@"../Images/Product/" + TenHinh);
                System.IO.File.Copy(id.HinhAnh, path, true);
            }
            int kq = id.CapNhatSanPhamMua(id, TenHinh, MaSanPhamMua);
            ArrayList list = LaySanPhamMua(MaSanPhamMua);
            ViewData["SPMua"] = list;
            return View();
        }

        public ArrayList LaySanPhamMua(int id)
        {
            Models.PostBuyRequest pbr = new Models.PostBuyRequest();
            ArrayList list = pbr.LaySanPhamMua(id);
            return list;
        }*/

        public ActionResult ProductQuote()
        {
            return View();
        }

     
        public ActionResult ViewPage1()
        {
            return View();
        }

    }
}
